package com.scisoft.scan.control.quickcheck;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.data.PartBean;
import com.scisoft.scan.data.depot.WarehouseBean;
import com.scisoft.scan.data.quickcheck.QuickCheckDetailBean;
import com.scisoft.scan.data.quickcheck.QuickCheckStockBean;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_QuickCheck;
import com.scisoft.scan.ws.WebServiceHelper_Stock;

import java.util.List;

public class CheckPartQueryActivity extends Activity {

    private ImageView iv_Back; //返回按钮
    private ListView lv_data;
    //配件的搜索条件
    private WarehouseBean depot;
    private String orderId = null;
    private PartBean partBean;
    private String condition;
    private String flag;
    private List<QuickCheckStockBean> quickCheckStockBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_check_part_query);
        //参数传递
        Intent intent = getIntent();
        depot = (WarehouseBean) intent.getSerializableExtra("depot");//仓库
        orderId = intent.getStringExtra("orderId");//单号
        flag = intent.getStringExtra("flag");//1 是点击放大镜2是 扫码
        condition = intent.getStringExtra("condition");//查询条件 当flag=1时有效
        partBean = (PartBean) intent.getSerializableExtra("part");//查询条件 当flag=2时有效

        //控件初始化
        lv_data = findViewById(R.id.lv_data);
        lv_data.setOnItemClickListener(new ListViewClick());
        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回
                Intent intent = new Intent();
                //设置返回数据
                setResult(RESULT_CANCELED, intent);
                //关闭Activity
                finish();
            }
        });

        //查询
        CommonUtil.showPress(this);
        QueryTask queryTask = new QueryTask(CheckPartQueryActivity.this);
        queryTask.execute();
    }

    //选择
    private class ListViewClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position >= 0 && position < quickCheckStockBeanList.size()) {
                QuickCheckStockBean selectData = quickCheckStockBeanList.get(position);
                //弹出对话框输入再返回
                QuickCheckDetailBean quickCheckDetailBean = new QuickCheckDetailBean();
                quickCheckDetailBean.checkCount = "0";
                quickCheckDetailBean.depotName = selectData.depotName;
                quickCheckDetailBean.imgUrl = selectData.imgUrl;
                quickCheckDetailBean.manufacturer = selectData.manufacturer;
                quickCheckDetailBean.manufacturerName = selectData.manufacturerName;
                quickCheckDetailBean.partCode = selectData.partCode;
                quickCheckDetailBean.stockId = selectData.stockId;
                quickCheckDetailBean.partId = selectData.partId;
                quickCheckDetailBean.partName = selectData.partName;
                quickCheckDetailBean.position = selectData.position;
                quickCheckDetailBean.stockCount = String.valueOf(selectData.stockCount);
                alert_edit(quickCheckDetailBean);
            }
        }
    }

    //自定义弹出窗口
    private void alert_edit(final QuickCheckDetailBean quickCheckDetailBean) {
        //1.创建一个Dialog对象,去除标题栏
        final Dialog mDialog = new Dialog(this);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //2.填充布局,将自定义布局设置进去
        LayoutInflater inflater = LayoutInflater.from(this);
        View dialogView = inflater.inflate(R.layout.dialog_check, null);
        mDialog.setContentView(dialogView);
        //3.设置指定的宽高,如果不设置的话，弹出的对话框可能不会显示全整个布局，当然在布局中写死宽高也可以
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = mDialog.getWindow();
        lp.copyFrom(window.getAttributes());
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //注意要在Dialog show之后，再将宽高属性设置进去，才有效果
        mDialog.show();
        window.setAttributes(lp);

        //4、设置点击其它地方不让消失弹窗
        mDialog.setCancelable(false);
        //5、初始化控件
        TextView tv_zms = (TextView) dialogView.findViewById(R.id.tv_zms);
        tv_zms.setText(String.valueOf(quickCheckDetailBean.stockCount));
        final TextView et_count = (TextView) dialogView.findViewById(R.id.et_count);
        TextView tv_equal = (TextView) dialogView.findViewById(R.id.tv_equal);
        tv_equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_count.setText(String.valueOf(quickCheckDetailBean.stockCount));
            }
        });
        TextView tv_none = (TextView) dialogView.findViewById(R.id.tv_none);
        tv_none.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_count.setText("0");
            }
        });
        TextView tv_ok = (TextView) dialogView.findViewById(R.id.tv_ok);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //按下确定键后的事件
                QuickCheckDetailBean newData = quickCheckDetailBean;
                newData.checkCount = et_count.getText().toString();

                CommonUtil.showPress(CheckPartQueryActivity.this);
                CommitTask commitTask = new CommitTask(CheckPartQueryActivity.this, newData);
                commitTask.execute();

                mDialog.dismiss();
            }
        });
        TextView tv_cancel = (TextView) dialogView.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
    }

    //绑定数据
    private void BindData(Context context) {
        //数据
        CheckPartStockAdapter adapter = new CheckPartStockAdapter(quickCheckStockBeanList, context);
        lv_data.setAdapter(adapter);
    }

    //查询库存服务
    class QueryTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        QueryTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                if ("2".equals(flag)) {
                    if (partBean != null && !"".equals(partBean.partId)) {
                        //查询列表数据
                        quickCheckStockBeanList = WebServiceHelper_QuickCheck.QUICKCHECK_01(depot, partBean.partCode, orderId);
                    }
                } else {
                    //查询列表数据
                    quickCheckStockBeanList = WebServiceHelper_QuickCheck.QUICKCHECK_01(depot, condition, orderId);
                }

                msg = "成功";

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //查询库存服务
    class CommitTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private String data = "";
        private QuickCheckDetailBean quickCheckDetailBean;

        CommitTask(Context context, QuickCheckDetailBean quickCheckDetailBean) {
            super();
            mcontext = context;
            this.data = data;
            this.quickCheckDetailBean = quickCheckDetailBean;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                //上传盘点数据
                String retOrderId = WebServiceHelper_QuickCheck.QUICKCHECK_02(quickCheckDetailBean, depot, orderId);
                if (retOrderId != null) {
                    orderId = retOrderId;
                    msg = "成功";
                } else {
                    msg = "上传失败";
                }

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                Intent intent = new Intent();
                intent.putExtra("data", orderId);
                setResult(RESULT_OK, intent);
                //关闭Activity
                finish();
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

}
